package com.wolfheader.wolfgame.controller.admin;

import java.io.File;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.wolfheader.wolfgame.Message;
import com.wolfheader.wolfgame.bean.admin.GameBean;
import com.wolfheader.wolfgame.controller.BaseController;
import com.wolfheader.wolfgame.dao.admin.GameDao;
import com.wolfheader.wolfgame.entity.admin.GameEntity;
import com.wolfheader.wolfgame.service.admin.AdminService;
import com.wolfheader.wolfgame.service.admin.GameService;

@Controller("adminGameController")
@RequestMapping("/admin/game")
public class GameController extends BaseController {

	private static final String template_url = "admin/game";

	/** 索引重定向URL */
	private static final String INDEX_REDIRECT_URL = "redirect:/admin/game";

	private static final String GAME_PATH = ":/home/wolfgame/game/";

	@Autowired
	private GameDao gameDao;

	@Resource(name = "adminServiceImpl")
	private AdminService adminService;

	@Resource(name = "gameServiceImpl")
	private GameService gameService;

	@RequestMapping(method = RequestMethod.GET)
	public String list(HttpServletRequest request, ModelMap map) {
		map.addAttribute("games", gameDao.findAll());
		map.addAttribute("menu1", "game");
		map.addAttribute("menu2", "list");
		return GameController.template_url + "/list";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(HttpServletRequest request, ModelMap map) {
		map.addAttribute("menu1", "game");
		map.addAttribute("menu2", "add");
		// 创建一个文件家
		return GameController.template_url + "/add";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addPermission(@Validated GameBean bean, HttpServletRequest request, RedirectAttributes attr) {
		try {
			GameEntity game = gameService.add(bean);
			File file = new File(GAME_PATH + game.getId());
			file.mkdirs();
			addFlashMessage(attr, Message.success("添加成功", INDEX_REDIRECT_URL));
		} catch (Exception e) {
			// TODO: handle exception
			addFlashMessage(attr, Message.error("添加失败"));
		}
		return INDEX_REDIRECT_URL;
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable Long id, RedirectAttributes attr, ModelMap map) {
		GameEntity entity = gameDao.findOne(id);
		if (entity == null) {
			addFlashMessage(attr, Message.error("游戏不存在"));
			return INDEX_REDIRECT_URL;
		}
		map.addAttribute("menu1", "game");
		map.addAttribute("menu2", "list");
		map.addAttribute("game", entity);
		return template_url + "/edit";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public String edit(@PathVariable Long id, @Validated GameBean bean, RedirectAttributes attr) {
/*		boolean result = permissionService.modif(id, bean);
		if (result) {
			addFlashMessage(attr, Message.success("修改成功"));
		} else {
			addFlashMessage(attr, Message.error("修改失败"));
		}*/
		return INDEX_REDIRECT_URL;
	}

	/*
	 * @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	 * public String add(@PathVariable Long id, HttpServletRequest request,
	 * RedirectAttributes attr) { permissionService.delete(id);
	 * addFlashMessage(attr, Message.success("删除成功")); return
	 * INDEX_REDIRECT_URL; }
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 */

}
